package com.wu.Backstage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wu.Backstage.entity.CommodityEntity;
import com.wu.Backstage.entity.CommodityEntityJp;
import com.wu.Backstage.entity.Po.CommodityEntityPo;
import com.wu.Backstage.entity.Po.ShoppingCartPo;
import com.wu.Backstage.entity.ShoppingCart;
import com.wu.Backstage.tool.PageHelperUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


@Service
public class CommodityService {

    @Resource
    private CommodityEntity commodityEntity;

    @Resource
    private CommodityEntityJp commodityEntityJp;

    @Resource
    private ShoppingCart shoppingCart;


    /**根据类别分类商品**/
    public List<CommodityEntityPo> findCommodityEntityType(String type,String region)
    {
        List<CommodityEntityPo> com = commodityEntityJp.findCommodityEntityType(type,region);
        return com;
    }


    /**根据id删除购物车商品**/
    public void DataShoppingCartPo(Integer id)
    {
        shoppingCart.deleteById(id);
    }


    /**商品添加**/
    public CommodityEntityPo CommodityEntity(CommodityEntityPo commodity)
    {
        CommodityEntityPo stores = commodityEntity.save(commodity);
        return stores;
    }




    /**商品添加**/
    public ShoppingCartPo AddShoppingCartPo(ShoppingCartPo shoppingCartPo)
    {
        ShoppingCartPo shopp =shoppingCart.save(shoppingCartPo);
        return shopp;
    }



    /**查询所有商品**/
    public List<CommodityEntityPo>  findByommodity()
    {
            List<CommodityEntityPo> commodity = (List<CommodityEntityPo>) commodityEntity.findAll();
            return commodity;
    }


    /**根据id删除商品**/
    public void  deletePerson(Integer id)
    {
        commodityEntity.deleteById(id);
    }

    /**根据id查询**/
    public Optional<CommodityEntityPo> findById(Integer id)
    {
        Optional<CommodityEntityPo> commodity = commodityEntity.findById(id);
        return commodity;
    }


    public List<CommodityEntityPo> findBystat(Integer sta,Integer st)
    {
        List<CommodityEntityPo> commodity = commodityEntityJp.findCommodityEntityPoSta(sta,st);
        return commodity;
    }


    /**根据id修改上架状态**/
    public int UpCommodityEntity(Integer id)
    {
        int com = commodityEntityJp.UpCommodityEntity(id);
        return com;
    }

    /**根据id修改下架状态**/
    public int UpCommodityEntitysta(Integer id)
    {
        int com = commodityEntityJp.UpCommodityEntitysta(id);
        return com;
    }


   public List<CommodityEntityPo> findCommodityEntityResource(String res){
       List<CommodityEntityPo> com = commodityEntityJp.findCommodityEntityResource(res);
       return com;
    }



    /**数据分页**/
    public PageHelperUtil<CommodityEntityPo> findBystatPage(Integer sta,Integer st,int page,int pageSize)
    {

        //起始页和每页要显示的数据
        PageHelper.startPage(page,pageSize);
        //查询所有信息
        List<CommodityEntityPo>  list = findByommodity();
        PageInfo<CommodityEntityPo> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo);
        PageHelperUtil<CommodityEntityPo> result = new PageHelperUtil<>();
        //结果集封装到当前页
        result.setPage(page);
        //每页数
        result.setPageSize(pageInfo.getPageSize());
        //总页数
        result.setPageTotal(pageInfo.getPages());
        //总条数
        result.setTotal(pageInfo.getTotal());
        //结果集
        result.setList(pageInfo.getList());

        return result;
    }









}
